package com.p900.website.web;

import com.p900.website.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * Created by lenovo on 2015/5/2.
 */
@Controller
public class ProductController {

    @Autowired
    private ProductService productService;

    @ResponseBody
    @RequestMapping("/product/listInPlatform")
    public Object findByPlatformId(Long platformId,
                                   @RequestParam(defaultValue = "0") Integer page,
                                   @RequestParam(defaultValue = "10") Integer size) {
        Assert.notNull(platformId, "platform id is null");
        return productService.findByPlatformId(platformId, new PageRequest(page, size));
    }

    @RequestMapping("/product/search")
    public String search(String key,
                         @RequestParam(defaultValue = "0") Integer page,
                         Map<String, Object> model){
        Assert.notNull(key, "search key is null");
        model.put("items", productService.searchProducts(key, new PageRequest(page, 10)));
        return "";
    }
}
